package dominion.kingdom;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import dominion.Card;
import dominion.CardTypes;
import dominion.Dominion;
import dominion.Player;
import dominion.Tresure;

public class Thief extends AbstractAttackAction {
	static final Logger LOG = LoggerFactory.getLogger(Thief.class);

	public Thief() {
	}

	@Override
	public void execute(Dominion dominion) {
		Player player = dominion.current();
		for (Player p : dominion.waitings()) {
			if (hasMoat(p) == false) {
				Card left = p.reveal(dominion);
				Card right = p.reveal(dominion);
				if (CardTypes.isTresure(left) && CardTypes.isTresure(right)) {
					Tresure lt = (Tresure) left;
					Tresure rt = (Tresure) right;
					if (lt.value() == rt.value() || rt.value() < lt.value()) {
						kickOver(player, p, lt);
						p.discard(rt);
					} else if (lt.value() < rt.value()) {
						kickOver(player, p, rt);
						p.discard(lt);
					}
				} else if (CardTypes.isTresure(left)) {
					kickOver(player, p, left);
					p.discard(right);
				} else if (CardTypes.isTresure(right)) {
					kickOver(player, p, right);
					p.discard(left);
				} else {
					if (left != null) {
						p.discard(left);
					}
					if (right != null) {
						p.discard(right);
					}
				}
			}
		}
	}

	private void kickOver(Player player, Player opponent, Card card) {
		opponent.trash(card);
		CardTypes ct = card.getType();
		if (CardTypes.Gold.equals(ct)
				|| (CardTypes.Silver.equals(ct) && player.coinRatio() < 1.5)) {
			player.gain(card);
		}
	}

	@Override
	public CardTypes getType() {
		return CardTypes.Thief;
	}

	@Override
	public String toString() {
		return getType().name();
	}
}
